/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * BeerInvUpdateFrame.java
 *
 * Created on Apr 8, 2011, 2:50:28 PM
 */

package gbbmgmt;

import java.util.*;
import org.jdesktop.application.Action;
import javax.swing.JOptionPane;
import javax.swing.table.*;
import java.text.*;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import java.awt.event.MouseEvent;
import javax.swing.*;
import java.awt.event.*;

/**
 *
 * @author dchapman
 */
public class BeerInvUpdateFrame extends javax.swing.JFrame {

    public ArrayList<String[]> Inv;

    /** Creates new form BeerInvUpdateFrame */
    public BeerInvUpdateFrame() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jButton1 = new javax.swing.JButton();
        jButtonExit = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setName("Form"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Beer Name", "1/2 BBL Kegs", "1/4 BBL Kegs", "1/6 BBL Kegs", "22 Oz. Bottles", "12 Oz. Bottles", "750 mL Bottles"
            }
        ));
        jTable1.setName("jTable1"); // NOI18N
        jScrollPane1.setViewportView(jTable1);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(gbbmgmt.GBBMGMTApp.class).getContext().getResourceMap(BeerInvUpdateFrame.class);
        jTable1.getColumnModel().getColumn(0).setMinWidth(150);
        jTable1.getColumnModel().getColumn(0).setPreferredWidth(150);
        jTable1.getColumnModel().getColumn(0).setMaxWidth(150);
        jTable1.getColumnModel().getColumn(0).setHeaderValue(resourceMap.getString("jTable1.columnModel.title0")); // NOI18N
        jTable1.getColumnModel().getColumn(1).setHeaderValue(resourceMap.getString("jTable1.columnModel.title1")); // NOI18N
        jTable1.getColumnModel().getColumn(2).setHeaderValue(resourceMap.getString("jTable1.columnModel.title2")); // NOI18N
        jTable1.getColumnModel().getColumn(3).setHeaderValue(resourceMap.getString("jTable1.columnModel.title3")); // NOI18N
        jTable1.getColumnModel().getColumn(4).setHeaderValue(resourceMap.getString("jTable1.columnModel.title4")); // NOI18N
        jTable1.getColumnModel().getColumn(5).setHeaderValue(resourceMap.getString("jTable1.columnModel.title5")); // NOI18N
        jTable1.getColumnModel().getColumn(6).setHeaderValue(resourceMap.getString("jTable1.columnModel.title6")); // NOI18N

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(gbbmgmt.GBBMGMTApp.class).getContext().getActionMap(BeerInvUpdateFrame.class, this);
        jButton1.setAction(actionMap.get("UpdateInv")); // NOI18N
        jButton1.setText(resourceMap.getString("jButton1.text")); // NOI18N
        jButton1.setName("jButton1"); // NOI18N

        jButtonExit.setAction(actionMap.get("ExitInv")); // NOI18N
        jButtonExit.setText(resourceMap.getString("jButtonExit.text")); // NOI18N
        jButtonExit.setName("jButtonExit"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jButton1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 548, Short.MAX_VALUE)
                .addComponent(jButtonExit)
                .addContainerGap())
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 728, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 357, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jButtonExit))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new BeerInvUpdateFrame().setVisible(true);
            }
        });
    }

    public void LoadTable(){
        //-------------------------
        //---Load the Malt Table---
        //-------------------------
        DefaultTableModel tabelModel = (DefaultTableModel)jTable1.getModel();
        for (int x=tabelModel.getRowCount()-1; x>=0; x--){
            tabelModel.removeRow(x);
        }

        DBA.openDBCon();
        Inv = DBA.RunSQLQuery("SELECT BIId,BIBId,BIHalf,BIQtr,BISix,BITT,BITW,BISF FROM BeerInventory");

        for(String s[] : Inv){
            DBA.GetBeerData(s[1]);
            String name = DBA.BName;
            tabelModel.addRow(new Object[]{name,s[2],s[3],s[4],s[5],s[6],s[7]});
        }

        DBA.closeDBCon();
    }

    @Action
    public void ExitInv() {
        dispose();
    }

    @Action
    public void UpdateInv() {
        int response = JOptionPane.showConfirmDialog(null,
                "This will modify inventory data in database, continue?",
                "Warning",JOptionPane.WARNING_MESSAGE);
        if (response == 0){
            DBA.openDBCon();
            DefaultTableModel tabelModel = (DefaultTableModel)jTable1.getModel();
            for(int x=tabelModel.getRowCount()-1; x>=0; x--){
                for(String s[] : Inv){
                    DBA.GetBeerData(s[1]);
                    String name = DBA.BName;
                    if(name.equals(jTable1.getValueAt(x, 0))){
                        if(!s[2].equals(jTable1.getValueAt(x, 1))){
                            String query = "UPDATE BeerInventory SET BIHalf='" +
                                    jTable1.getValueAt(x, 1) + "' WHERE BIId='" +
                                    s[0] + "'";
                            DBA.RunSQLUpdateQuery(query);
                        }
                        if(!s[3].equals(jTable1.getValueAt(x, 2))){
                            String query = "UPDATE BeerInventory SET BIQtr='" +
                                    jTable1.getValueAt(x, 2) + "' WHERE BIId='" +
                                    s[0] + "'";
                            DBA.RunSQLUpdateQuery(query);
                        }
                        if(!s[4].equals(jTable1.getValueAt(x, 3))){
                            String query = "UPDATE BeerInventory SET BISix='" +
                                    jTable1.getValueAt(x, 3) + "' WHERE BIId='" +
                                    s[0] + "'";
                            DBA.RunSQLUpdateQuery(query);
                        }
                        if(!s[5].equals(jTable1.getValueAt(x, 4))){
                            String query = "UPDATE BeerInventory SET BITT='" +
                                    jTable1.getValueAt(x, 4) + "' WHERE BIId='" +
                                    s[0] + "'";
                            DBA.RunSQLUpdateQuery(query);
                        }
                        if(!s[6].equals(jTable1.getValueAt(x, 5))){
                            String query = "UPDATE BeerInventory SET BITW='" +
                                    jTable1.getValueAt(x, 5) + "' WHERE BIId='" +
                                    s[0] + "'";
                            DBA.RunSQLUpdateQuery(query);
                        }
                        if(!s[7].equals(jTable1.getValueAt(x, 6))){
                            String query = "UPDATE BeerInventory SET BISF='" +
                                    jTable1.getValueAt(x, 6) + "' WHERE BIId='" +
                                    s[0] + "'";
                            DBA.RunSQLUpdateQuery(query);
                        }
                    }
                }
            }
            DBA.closeDBCon();
        }
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButtonExit;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    // End of variables declaration//GEN-END:variables

}
